<template>
    <div>
        <Title :title="title"></Title>
        <div ref="bottom3" style="height: 150px;"></div>
    </div>
</template>
<script>
import Title from './Title.vue';
import { Rose } from '@antv/g2plot';
import { get } from '@/utils/request';
import moment from 'moment';
import { mapState } from 'vuex';
export default {
    data() {
        return {
            title: '风速',
             params: {
                page: 1,
                pageSize: 200
            },
            fData:[],
            rose:null
        }
    },
    computed: {
        ...mapState(['ed'])
    },
     watch: {
        ed: {
            handler(n, o) {
                // 根据设备信息重新查询数据 重新渲染图表 
                this.params.engineer_id = n.engineer_id;
                this.params.device_id = n.device_id;
                this.getFData();
                // 根据最新获取的数据重新渲染图表 
                this.rose.changeData(this.fData);
            },
            // 深度监听 
            deep: true
        }
    },
    methods: {
        loadRose() {
            this.rose = new Rose(this.$refs.bottom3, {
                data:this.fData,
                xField: 'insert_time',
                yField: 'noise',
                autoFit: true,
                seriesField: null,
                radius: 1,
                color:'yellow',
                innerRadius:0.1,
                tooltip: { showMarkers: false,
                    domStyles:{
                        "g2-tooltip":{
                            background:'#147',
                            color:"white",
                            fontSize:20
                        }
                    }
                 },
                state: {
                    active: {
                        style: {
                            shadowBlur: 4,
                            stroke: '#000',
                            fill: 'red',
                        },
                    },
                },
                // 设置动画
                animation: {
                    appear: {
                        animation: 'zoom-in',
                        duration: 5000,
                    },
                },
                xAxis:{
                    label:{
                        style:{
                            fill:'#FF8C94',
                            fontSize:14
                        }
                    }
                },
                label: {
                    style: {
                        fill: 'red',
                        fontSize:0
                    }
                },
                legend: {
                    position: 'bottom',
                },
            });

            this.rose.render();

        },
        async getFData(){
            let res = await new get('/dashboard/pageQueryData',this.params);
            let temp = [];
            // 将后端获取的数据设置给tempData 
            res.data.list.forEach((item, index, arr) => {
                // item表示数组每一项 
                item.insert_time = moment(item.insert_time).format("HH:mm");
                // 由于查询数据密度密集 需要将数据处理 每隔10条数据取一条数据 
                if (index % 10 == 0) {
                    temp.push(arr[index])
                }
            });
            this.fData = temp;
        }
    },
    async mounted() {
        await this.getFData();
        this.loadRose();
    }
}
</script>
<style lang="less" scoped></style>